<?php
namespace app\models;

use fastphp\base\Model;

class RemoteJudge extends Model
{
    var $url;
    var $cookie;
    var $header;
    var $refer;
    var $problem;
    var $curl;
    var $postdata;
    var $language = array (
        0 => '自动识别语言',
        1 => 'Pascal',
        2 => 'C',
        3 => 'C++',
        4 => 'C++11',
        11 => 'C++14',
        12 => 'C++17',
        6 => 'Python 2',
        7 => 'Python 3',
        24 => 'PyPy 2',
        25 => 'PyPy 3',
        8 => 'Java 8',
        9 => 'Node.js',
        13 => 'Ruby',
        14 => 'Go',
        15 => 'Rust',
        16 => 'PHP 7',
        17 => 'C# Mono',
        18 => 'Visual Basic Mono',
        19 => 'Haskell',
        20 => 'Kotlin/Native',
        21 => 'Kotlin/JVM',
        22 => 'Scala',
        23 => 'Perl'
    );
    public static function init($config, $problem, $postdata)
    {
        if (!is_array($config)) {
            return false;
        }
        $this->url = @$config['url'];
        $this->cookie = @$config['cookie'];
        $this->header = @$config['header'];
        $this->refer = @$config['refer'];
        $this->postdata = $postdata;
        $this->problem = $problem;
        if ($this->cookie)
            $this->header['cookie'] = $this->cookie;
        return true;
    }
    public static function start()
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->url);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->postdata);
        if (($curlResult = curl_exec($curl)) === false)
            return false;
        return $curlResult;
    }
}